Design Patterns
Credits: https://refactoring.guru/design-patterns
Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code.
Design patterns differ by their complexity, level of detail and scale of applicability to the entire system being designed. I like the analogy to road construction: you can make an intersection safer by either installing some traffic lights or building an entire multi-level interchange with underground passages for pedestrians.
The most basic and low-level patterns are often called idioms. They usually apply only to a single programming language.
The most universal and high-level patterns are architectural patterns. Developers can implement these patterns in virtually any language. Unlike other patterns, they can be used to design the architecture of an entire application.
In addition, all patterns can be categorized by their intent, or purpose. This book covers three main groups of patterns:
Creational patterns provide object creation mechanisms that increase flexibility and reuse of existing code.
Structural patterns explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient.
Behavioral patterns take care of effective communication and the assignment of responsibilities between objects.
22 Design Patterns
https://refactoring.guru/design-patterns/catalog
Creational :
- Provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created
- Provides an interface for creating families of related objects, without specifying their concrete classes
- Separates the construction of a complex object from its representation, allowing the same construction process to create different representation
- Creates new objects by cloning an existing object rather than creating a new instance from scratch
- Ensures that only one instance of a class is created and provides a global point of access to that instance
Behavioral:
- Allows multiple objects to handle a request with each object deciding whether to handle the request or pass it on to the next object
- Encapsulates a request as an object, allowing the request to be parameterized with different arguments, queued or logged, and undone if necessary
- Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation
- Defines an object that encapsulates how a set of objects interact, promoting loose coupling between those objects.
- Provides the ability to restore an object to its previous state (undo)
- Defines a one-to-many dependency between objects, so that when one object changes states, all its dependents are notified and updated automatically
- Allow an object to alter its behavior when its internal state changes
-Defines a family of algorithms , encapsulates each one and makes them interchangeable. It lets the algorithm vary independently from clients that use it
- Defines a skeleton of an algorithm in a base class allowing subclasses to provide specific behavior for some of the steps
- Separates an algorithm from an object structure on which it operates. The algorithm can then be changed without changing the object structure
Structural:
- Converts the interface of a class into another interface that clients expect. It allows incompatible class to work together
- Decouples an abstraction from its implementation so that two can vary independently
- Composes objects into tree structures to represent whole-part hierarchies. It lets clients treat individual objects as compositions of objects uniformly
- Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
- Provides a simplified interface to a complex subsystem, making it easier to use and understand.
- Shares objects to reduce memory usage. It is useful when many identical object must be created
- Provides a surrogate or placeholder for another object to control access to it